package com.cspg.design.request;

import com.alibaba.fastjson.JSONObject;
import com.cspg.design.bean.UserInfo;
import com.cspg.design.common.Constant;
import com.cspg.design.dto.AuthUserDto;
import com.cspg.design.http.IService;
import com.cspg.design.http.RetrofitUtil;
import com.cspg.design.model.ProjectModel;
import com.cspg.design.model.UserModel;
import com.cspg.design.presenter.HomeFragmentPresenter;
import com.cspg.design.presenter.LoginPresenter;
import com.cspg.design.utils.RSAUtils;

import java.util.Date;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Home {

    private Call call;
    private HomeFragmentPresenter presenter;

    public Home(HomeFragmentPresenter presenter) {
        this.presenter = presenter;
    }

    public void getProject() throws Exception
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        String unit = UserInfo.getInstance().unit;
        if (UserInfo.getInstance().role.contains("设计"))
        {
            call = retrofitUtil.createReq(IService.class).designProject(0,1000,unit);
        }
        else
        {
            call = retrofitUtil.createReq(IService.class).constructionProject(0,1000,unit);
        }
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                ProjectModel projectModel= (ProjectModel) response.body();
                if (null == projectModel) {
                    presenter.getFailure(Constant.HTTP_REQUEST_NET_WORK_ERROR);
                    return;
                }
                else
                {
                    presenter.requestSuccess(projectModel);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }
}
